package cn.edu.zzu.demo01Thread;

public class RaceThread implements Runnable {

    private static String Winner;

    public static void main(String[] args) {
        new Thread(new RaceThread(), "兔子").start();
        new Thread(new RaceThread(), "乌龟").start();
    }

    @Override
    public void run() {
        for (int i = 0; i <= 100; i++) {

            //兔子睡
            if(Thread.currentThread().getName().equals("兔子")&& i%10==0){
                try {
                    Thread.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

            if (GameOver(i)) {
                break;
            }
            System.out.println(Thread.currentThread().getName() + "==>跑了" + i + "步！");
        }
    }

    /**
     * 胜利者
     *
     * @param steps
     * @return
     */
    public Boolean GameOver(int steps) {
        if (Winner != null) {
            return true;
        }
        if (steps >= 100) {
            Winner = Thread.currentThread().getName();
            System.out.println("胜利者：" + Winner);
            return true;
        }

        return false;
    }
}
